package no.ntnu.idi.evaluering;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class Evaluation {
	
	private final static int COLUMN_SIZE = 14;
	private final static int COLUMN_SIZE_CALCDOCUMENT = 40;
	private final static int INFO_COLUMN_SIZE = 25;
	
	private List<EvaluationList> mTestResultsList;
	private EvaluationList mGoldCorpusList;
	
	public Evaluation(EvaluationList goldCorpusList, List<EvaluationList> testResultList) {
		mTestResultsList = new ArrayList<EvaluationList>(testResultList);
		mGoldCorpusList = goldCorpusList;
	}
	
	public Evaluation(EvaluationList goldCorpusList) {
		mTestResultsList = new ArrayList<EvaluationList>();
		mGoldCorpusList = goldCorpusList;
	}
	
	public void addNewTestResult(EvaluationList testResultList) {
		mTestResultsList.add(testResultList);
	}
	
	public void generateTestResultFile(File output) {
		Writer writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(output), "utf-8"));
			
			for(EvaluationList list: mTestResultsList) {
				EvaluationScore score = list.evaluateAgainst(mGoldCorpusList);
				
				writer.write(buildDividor("-", COLUMN_SIZE_CALCDOCUMENT)+"\n");
				writer.write(fixedLengthString("Filenavn: "+list.getName(), COLUMN_SIZE_CALCDOCUMENT)+"|\n");
				writer.write(fixedLengthString("Recall: "+score.calculateRecall(), COLUMN_SIZE_CALCDOCUMENT)+"|\n");
				writer.write(fixedLengthString("Precision: "+score.calculatePrecision(), COLUMN_SIZE_CALCDOCUMENT)+"|\n");
				writer.write(fixedLengthString("Fallout: "+score.calculateFallout(), COLUMN_SIZE_CALCDOCUMENT)+"|\n");
				writer.write(fixedLengthString("F-measure: "+score.calculateFMeasure(1.0), COLUMN_SIZE_CALCDOCUMENT)+"|\n");
				writer.write(buildDividor("-", COLUMN_SIZE_CALCDOCUMENT)+"\n");
			}
			writer.close();
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
				try {
					writer.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
		}
	}
	
	
	public void generateComparableResultFile(File output) {
		Writer writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(output), "utf-8"));
			String header = generateHeader();
			writer.write(header+"\n");
			writer.write(buildDividor("-", header.length())+"\n");
			for(int i = 0; i< mGoldCorpusList.size(); i++) {
				String line = fixedLengthString(mGoldCorpusList.get(i).getKey(),INFO_COLUMN_SIZE)+"|";
				line += fixedLengthString(mGoldCorpusList.get(i).isSensitivToString(),COLUMN_SIZE)+"|";
				for(EvaluationList list: mTestResultsList) {
					line += fixedLengthString(list.get(i).isSensitivToString(),COLUMN_SIZE)+"|";
				}
				writer.write(line+"\n");
			}
			writer.close();
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
				try {
					writer.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
		}
	}
	
	public String generateHeader() {
		String headerString = fixedLengthString("", INFO_COLUMN_SIZE)+"|";
		headerString += fixedLengthString(mGoldCorpusList.getName(), COLUMN_SIZE)+"|";
		for(EvaluationList list: mTestResultsList) {
			headerString += fixedLengthString(list.getName(), COLUMN_SIZE)+"|";
		}
		return headerString;
	}
	
	public static String buildDividor(String str, int length){
		String result = "";
		for(int i = 0; i < length; i++) {
			result += str;
		}
		return result;
	}
	
	public static String fixedLengthString(String string, int length) {
	    return String.format("%-"+length+ "s", string);
	}
	
}
